home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_color_win32.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-13  |  5.8 KB  |  211 lines

  1. //
  2. // "$Id: fl_color_win32.cxx,v 1.14 1999/01/13 15:56:23 mike Exp $"
  3. //
  4. // WIN32 color functions for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // The fltk "colormap".  This allows ui colors to be stored in 8-bit
  27. // locations, and provides a level of indirection so that global color
  28. // changes can be made.  Not to be confused with the X colormap, which
  29. // I try to hide completely.
  30.  
  31. // SGI compiler seems to have problems with unsigned char arguments
  32. // being used to index arrays.  So I always copy them to an integer
  33. // before use.
  34.  
  35. #include <config.h>
  36. #include <FL/Fl.H>
  37. #include <FL/win32.H>
  38. #include <FL/fl_draw.H>
  39.  
  40. static unsigned fl_cmap[256] = {
  41. #include "fl_cmap.h" // this is a file produced by "cmap.cxx":
  42. };
  43.  
  44. // Translations to win32 data structures:
  45. Fl_XMap fl_xmap[256];
  46.  
  47. Fl_XMap* fl_current_xmap;
  48.  
  49. HPALETTE fl_palette;
  50. HPEN tmppen=0;
  51. HBRUSH tmpbrush=0;
  52.  
  53. static void clear_xmap(Fl_XMap& xmap) {
  54.   if (xmap.pen) {
  55.     if(!tmppen) tmppen = CreatePen(PS_SOLID, 1, 0);
  56.     if(!tmpbrush) tmpbrush = CreateSolidBrush(0);
  57.     HPEN oldpen = (HPEN)SelectObject(fl_gc, tmppen); // Push out the current pen of the gc
  58.     if(oldpen != xmap.pen) SelectObject(fl_gc, oldpen); // Put it back if it is not the one we are about to delete
  59.     SelectObject(fl_gc, tmpbrush); // Push out the old pen of the gc
  60.     //fl_current_xmap = 0;
  61.     DeleteObject((HGDIOBJ)(xmap.pen));
  62.     xmap.pen = 0;
  63.     xmap.brush = -1;
  64.   }
  65. }
  66.  
  67. static void set_xmap(Fl_XMap& xmap, COLORREF c) {
  68.   xmap.rgb = c;
  69.   xmap.pen = CreatePen(PS_SOLID, 1, xmap.rgb);
  70.   xmap.brush = -1;
  71. }
  72.  
  73. Fl_Color fl_color_;
  74.  
  75. void fl_color(Fl_Color i) {
  76.   fl_color_ = i;
  77.   Fl_XMap &xmap = fl_xmap[i];
  78.   if (!xmap.pen) {
  79. #if USE_COLORMAP
  80.     if (fl_palette) {
  81.       set_xmap(xmap, PALETTEINDEX(i));
  82.     } else {
  83. #endif
  84.       unsigned c = fl_cmap[i];
  85.       set_xmap(xmap, RGB(uchar(c>>24), uchar(c>>16), uchar(c>>8)));
  86. #if USE_COLORMAP
  87.     }
  88. #endif
  89.   }
  90.   fl_current_xmap = ⟼
  91.   SelectObject(fl_gc, (HGDIOBJ)(xmap.pen));
  92. }
  93.  
  94. void fl_color(uchar r, uchar g, uchar b) {
  95.   static Fl_XMap xmap;
  96.   COLORREF c = RGB(r,g,b);
  97.   if (!xmap.pen || c != xmap.rgb) {
  98.     clear_xmap(xmap);
  99.     set_xmap(xmap, c);
  100.   }
  101.   fl_current_xmap = ⟼
  102.   SelectObject(fl_gc, (HGDIOBJ)(xmap.pen));
  103. }
  104.  
  105. HBRUSH fl_brush() {
  106.   Fl_XMap *xmap = fl_current_xmap;
  107.   // Wonko: we use some statistics to cache only a limited number
  108.   // of brushes:
  109. #define FL_N_BRUSH 16
  110.   static struct Fl_Brush {
  111.     HBRUSH brush;
  112.     unsigned short usage;
  113.     Fl_XMap* backref;
  114.   } brushes[FL_N_BRUSH];
  115.  
  116.   int i = xmap->brush; // find the associated brush
  117.   if (i != -1) { // if the brush was allready allocated
  118.     if (brushes[i].brush == NULL) goto CREATE_BRUSH;
  119.     if ( (++brushes[i].usage) > 32000 ) { // keep a usage statistic
  120.       for (int j=0; j<FL_N_BRUSH; j++) {
  121.     if (brushes[j].usage>16000)
  122.       brushes[j].usage -= 16000;
  123.     else 
  124.       brushes[j].usage = 0;
  125.       }
  126.     }
  127.     return brushes[i].brush;
  128.   } else {
  129.     int umin = 32000, imin = 0;
  130.     for (i=0; i<FL_N_BRUSH; i++) {
  131.       if (brushes[i].brush == NULL) goto CREATE_BRUSH;
  132.       if (brushes[i].usage<umin) {
  133.     umin = brushes[i].usage;
  134.     imin = i;
  135.       }
  136.     }
  137.     i = imin;
  138.     DeleteObject(brushes[i].brush);
  139.     brushes[i].brush = NULL;
  140.     brushes[i].backref->brush = -1;
  141.   }
  142. CREATE_BRUSH:
  143.   brushes[i].brush = CreateSolidBrush(xmap->rgb);
  144.   brushes[i].usage = 0;
  145.   brushes[i].backref = xmap;
  146.   xmap->brush = i;
  147.   return brushes[i].brush;
  148. }
  149.  
  150. void Fl::free_color(Fl_Color i, int overlay) {
  151.   if (overlay) return; // do something about GL overlay?
  152.   clear_xmap(fl_xmap[i]);
  153. }
  154.  
  155. void Fl::set_color(Fl_Color i, unsigned c) {
  156.   if (fl_cmap[i] != c) {
  157.     clear_xmap(fl_xmap[i]);
  158.     fl_cmap[i] = c;
  159.   }
  160. }
  161.  
  162. #if USE_COLORMAP
  163.  
  164. // 'fl_select_palette()' - Make a color palette for 8-bit displays if necessary
  165. // Thanks to Michael Sweet @ Easy Software Products for this
  166.  
  167. HPALETTE
  168. fl_select_palette(void)
  169. {
  170.   static char beenhere;
  171.   if (!beenhere) {
  172.     beenhere = 1;
  173.  
  174.     //if (GetDeviceCaps(fl_gc, BITSPIXEL) > 8) return NULL;
  175.     int nColors = GetDeviceCaps(fl_gc, SIZEPALETTE);
  176.     if (nColors <= 0 || nColors > 256) return NULL;
  177.     // this will try to work on < 256 color screens, but will probably
  178.     // come out quite badly.
  179.  
  180.     // I lamely try to get this variable-sized object allocated on stack:
  181.     ulong foo[(sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY))/sizeof(ulong)+1];
  182.     LOGPALETTE *pPal = (LOGPALETTE*)foo;
  183.  
  184.     pPal->palVersion    = 0x300;
  185.     pPal->palNumEntries = nColors;
  186.  
  187.     // Build 256 colors from the standard FLTK colormap...
  188.  
  189.     for (int i = 0; i < nColors; i ++) {
  190.       pPal->palPalEntry[i].peRed   = (fl_cmap[i] >> 24) & 255;
  191.       pPal->palPalEntry[i].peGreen = (fl_cmap[i] >> 16) & 255;
  192.       pPal->palPalEntry[i].peBlue  = (fl_cmap[i] >>  8) & 255;
  193.       pPal->palPalEntry[i].peFlags = 0;
  194.     };
  195.  
  196.     // Create the palette:
  197.     fl_palette = CreatePalette(pPal);
  198.   }
  199.   if (fl_palette) {
  200.     SelectPalette(fl_gc, fl_palette, FALSE);
  201.     RealizePalette(fl_gc);
  202.   }
  203.   return fl_palette;
  204. }
  205.  
  206. #endif
  207.  
  208. //
  209. // End of "$Id: fl_color_win32.cxx,v 1.14 1999/01/13 15:56:23 mike Exp $".
  210. //
  211.